/* Copyright (C) 2003, Charles Wang.
 * Author:  Charles Wang <charles@linux.net.cn>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 1
int magicbooter_main(int argc, char **argv);
/* in busybox applets.c */
int bbapplets_main(int argc, char **argv);

int main(int argc, char *argv[]) {
    char *applet_name;
    
    /* first invoke busybox applets */
    bbapplets_main(argc, argv);

    /* if not exit, try our applets */
    applet_name = strrchr(argv[0], '/');
    if (applet_name)
        applet_name++;
    else
        applet_name = argv[0];
        
    if (!strcmp(applet_name, "magicbooter"))
        return magicbooter_main(argc, argv);

    fprintf(stderr, "applet not found\n");
    return -1;
}

#else
char *applet_name;
const char *bb_applet_name;
void show_usage(void) {}

typedef int (*applet_main)(int, char *argv[]);
typedef struct {
    const char  *appname;
    applet_main  appmain;
}  appinfo;

int magicbooter_main(int argc, char *argv[]);
/*  Refer the README in this directory before add/remove of the included applets. */
int cpio_main(int argc, char *argv[]);
int gunzip_main(int argc, char *argv[]);
int ifconfig_main(int argc, char *argv[]);
int init_main(int argc, char *argv[]);
int insmod_main(int argc, char *argv[]);
int modprobe_main(int argc, char *argv[]);
int mount_main(int argc, char *argv[]);
int route_main(int argc, char *argv[]);
int rmmod_main(int argc, char *argv[]);
int lash_main(int argc, char *argv[]);
int umount_main(int argc, char *argv[]);
int wget_main(int argc, char *argv[]);
int ls_main(int argc, char *argv[]);
int cat_main(int argc, char *argv[]);
int mv_main(int argc, char *argv[]);
int mdev_main(int argc, char *argv[]);

const char * detect_hardware_runner(void);

appinfo applet_list[] = {
    { "magicbooter", magicbooter_main },
    { "cpio",      cpio_main },
    { "gunzip",    gunzip_main },
    { "ifconfig",  ifconfig_main },
    { "init",      init_main },
    { "insmod",    insmod_main },
    { "modprobe",  modprobe_main },
    { "mount",     mount_main },
    { "rmmod",     rmmod_main },
    { "route",     route_main },
    { "sh",        lash_main },
    { "umount",    umount_main },
    { "wget",      wget_main },
    { "ls",        ls_main },
    { "cat",       cat_main },
    { "mv",        mv_main },
    { "mdev",      mdev_main },
};

int main(int argc, char *argv[]) {
    int       found;
    int       ret;
    char     *arg0;
    appinfo  *ai;

    found = 0;
    arg0 = strdup(argv[0]);  applet_name = basename(arg0);
    bb_applet_name = applet_name;
    for (ai = applet_list;
	 ai - applet_list < sizeof(applet_list) / sizeof(applet_list[0]);
	 ++ai)
	if (!strcmp(ai->appname, applet_name)) {
	    ret = ai->appmain(argc, argv);
	    found = 1;
	    break;
	}
    if (!found)  fprintf(stderr, "Not supported applet %s.\n", applet_name);
    free(arg0);
    return  ret;
}

/* dummy stuff to make busybox happy */
//int been_there_done_that = 0; /* Also used in applets.c */
const char *bb_applet_name;

int busybox_main(int argc, char *argv[])
{
    return 0;
}
#endif  /* 0 */
